/*
 * GrouponEntity.java
 */
package Entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;

@Entity
public class GroupBuyingItem implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Double originalPrice;
    private Double offeredPrice;
    @Temporal(value = TemporalType.DATE)
    private Date effectiveEnd;
    @Temporal(value = TemporalType.DATE)
    private Date effectiveStart;
    @Temporal(value = TemporalType.DATE)
    private Date redemptionEnd;
    @Temporal(value = TemporalType.DATE)
    private Date redemptionStart;
    private Integer minQuota;
    private Integer maxQuota;
    private Integer personalQuota;
    private Boolean requireBooking;
    private String validateRequirements;
    private String description;
    private String name;
    private Boolean isApproved;
//    private Boolean isVoided;
    @Temporal(value = TemporalType.DATE)
    private Date submissionTime;
    @ManyToOne(cascade = {CascadeType.PERSIST})
    private Stakeholder stakeholder;
    @OneToMany(cascade = {CascadeType.PERSIST})
    private List<PurchasedItem_GroupOn> purchases;

    public GroupBuyingItem() {
        submissionTime = new Date();
    }

    public void create(
            Double originalPrice,
            Double offeredPrice,
            Date effectiveEnd,
            Date effectiveStart,
            Date redemptionEnd,
            Date redemptionStart,
            Integer minQuota,
            Integer maxQuota,
            Integer personalQuota,
            Boolean requireBooking,
            String description,
            String validateRequirements,
            String name, Stakeholder stakeholder) {
        setOriginalPrice(originalPrice);
        setOfferedPrice(offeredPrice);
        setEffectiveEnd(effectiveEnd);
        setEffectiveStart(effectiveStart);
        setRedemptionEnd(redemptionEnd);
        setRedemptionStart(redemptionStart);
        setMinQuota(minQuota);
        setMaxQuota(maxQuota);
        setPersonalQuota(personalQuota);
        setRequireBooking(requireBooking);
        setDescription(description);
        setValidateRequirements(validateRequirements);
        setName(name);
        setStakeholder(stakeholder);
        purchases = new ArrayList<PurchasedItem_GroupOn>();
    }

    public String getStatus() {
        if (this.isApproved == null) {
            return "pending";
        } else if (this.isApproved) {
            return "approved";
        } else {
            return "denied";
        }
    }

//    public String getEffectiveStartStr() {
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//        Date copyDate = (Date) effectiveStart.clone();
//        return sdf.format(copyDate);
//    }
//    public String getEffectiveEndStr() {
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//        Date copyDate = (Date) effectiveEnd.clone();
//        return sdf.format(copyDate);
//    }
//
//    public String getRedemptionStartStr() {
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//        Date copyDate = (Date) redemptionStart.clone();
//        return sdf.format(copyDate);
//    }
//
//    public String getRedemptionEndStr() {
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//        Date copyDate = (Date) redemptionEnd.clone();
//        return sdf.format(copyDate);
//    }
//
//    public String getSubmissionTimeStr(){
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy  hh:mm:ss");
//        Date copyDate = (Date) submissionTime.clone();
//        return sdf.format(copyDate);
//    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getOriginalPrice() {
        return originalPrice;
    }

    public void setOriginalPrice(Double originalPrice) {
        this.originalPrice = originalPrice;
    }

    public Double getOfferedPrice() {
        return offeredPrice;
    }

    public void setOfferedPrice(Double offeredPrice) {
        this.offeredPrice = offeredPrice;
    }

    public Date getRedemptionEnd() {
        return redemptionEnd;
    }

    public void setRedemptionEnd(Date redemptionEnd) {
        this.redemptionEnd = redemptionEnd;
    }

    public Date getRedemptionStart() {
        return redemptionStart;
    }

    public void setRedemptionStart(Date redemptionStart) {
        this.redemptionStart = redemptionStart;
    }

    public Integer getMinQuota() {
        return minQuota;
    }

    public void setMinQuota(Integer minQuota) {
        this.minQuota = minQuota;
    }

    public Integer getMaxQuota() {
        return maxQuota;
    }

    public void setMaxQuota(Integer maxQuota) {
        this.maxQuota = maxQuota;
    }

    public Boolean getRequireBooking() {
        return requireBooking;
    }

    public void setRequireBooking(Boolean requireBooking) {
        this.requireBooking = requireBooking;
    }

    public String getValidateRequirements() {
        return validateRequirements;
    }

    public void setValidateRequirements(String validateRequirements) {
        this.validateRequirements = validateRequirements;
    }

    public Stakeholder getStakeholder() {
        return stakeholder;
    }

    public void setStakeholder(Stakeholder stakeholder) {
        this.stakeholder = stakeholder;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getEffectiveEnd() {
        return effectiveEnd;
    }

    public void setEffectiveEnd(Date effectiveEnd) {
        this.effectiveEnd = effectiveEnd;
    }

    public Date getEffectiveStart() {
        return effectiveStart;
    }

    public void setEffectiveStart(Date effectiveStart) {
        this.effectiveStart = effectiveStart;
    }

    public Boolean getIsApproved() {
        return isApproved;
    }

    public void setIsApproved(Boolean isApproved) {
        this.isApproved = isApproved;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getSubmissionTime() {
        return submissionTime;
    }

    public void setSubmissionTime(Date submissionTime) {
        this.submissionTime = submissionTime;
    }

    public Integer getPersonalQuota() {
        return personalQuota;
    }

    public void setPersonalQuota(Integer personalQuota) {
        this.personalQuota = personalQuota;
    }

    public List<PurchasedItem_GroupOn> getPurchases() {
        return purchases;
    }

    public void setPurchases(List<PurchasedItem_GroupOn> purchases) {
        this.purchases = purchases;
    }

    public void addPurchase(PurchasedItem_GroupOn purchase) {
        for (PurchasedItem_GroupOn p : this.purchases) {
            if (p.getCreditCard().trim().equalsIgnoreCase(purchase.getCreditCard().trim())
                    && p.getCustomerId().longValue() == purchase.getCustomerId().longValue()) {
                p.addQty(purchase.getQty());
                return;
            }
        }
        this.purchases.add(purchase);
    }
}
